"use strict";

import Vue from 'vue';
import axios from "axios";
import {Message, MessageBox} from "element-ui";

// Full config:  https://github.com/axios/axios#request-config
// axios.defaults.baseURL = process.env.baseURL || process.env.apiUrl || '';
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;';

let config = {
  // baseURL: process.env.baseURL || process.env.apiUrl || ""
  // timeout: 60 * 1000, // Timeout
  // withCredentials: true, // Check cross-site Access-Control
  // 自动携带
  baseURL: process.env.VUE_APP_BASE_API
};

const _axios = axios.create(config);

//请求拦截器，统一将token带给后端
_axios.interceptors.request.use(
  function(config) {
    // Do something before request is sent
    var token = sessionStorage.getItem("token");
    if(token){
      config.headers['token'] = token
    }
    return config;
  },
  function(error) {
    // Do something with request error
    return Promise.reject(error);
  }
);

// Add a response interceptor
//响应拦截器
_axios.interceptors.response.use(
  function(response) {
    // Do something with response data
    if(response.data.code == 400){
      MessageBox.confirm(response.data.message,"系统提示",{
            confirmButtonText:'重新登录',
            cancelButtonText:'取消',
            type:'warning'
          }
      ).then(()=>{
        location.href="/";
      })
      // 阻止进一步链式调用 没提示任何信息 不走then
      return new Promise(() => {});
    }else if(response.data.code == 500){
      Message.error(response.data.message)
      // 阻止进一步链式调用 没提示任何信息 不走then
      return new Promise(() => {});
    }else{
      //正常情况，then后处理
      return response.data;
    }

  },
  function(error) {
    // Do something with response error
    return Promise.reject(error);
  }
);

Plugin.install = function(Vue) {
  Vue.axios = _axios;
  window.axios = _axios;
  Object.defineProperties(Vue.prototype, {
    axios: {
      get() {
        return _axios;
      }
    },
    $axios: {
      get() {
        return _axios;
      }
    },
  });
};

Vue.use(Plugin)

export default Plugin;
export {_axios as request}